<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  header("location:checklogin.php");
}

$driverArray = array();
if(isset($_POST['insertBtn']))
{  
	 $licenseNo  = (isset($_POST['licenseNo'])) && ($_POST['licenseNo'] != "") ? $_POST['licenseNo'] : 0 ;
	 $name       = (isset($_POST['name'])) && ($_POST['name'] != "") ? $_POST['name'] : "" ;
	 $address    = (isset($_POST['address'])) && ($_POST['address'] != "") ? $_POST['address'] : "" ;
	 $telephone  = (isset($_POST['telephone'])) && ($_POST['telephone'] != "") ? $_POST['telephone'] : "" ;
	 $mobile     = (isset($_POST['mobile'])) && ($_POST['mobile'] != "") ? $_POST['mobile'] : "" ;
	 $truckNo     = (isset($_POST['truckNo'])) && ($_POST['truckNo'] != "") ? $_POST['truckNo'] : "" ;

  $insertQuerydriver = "INSERT INTO driver (licenseNo,driverName,truckId,address,telephone,mobile)
                             VALUES('".$licenseNo."',
                            			  '".$name."',
                            			  '".$truckNo."',
                            			  '".$address."',
                            			  '".$telephone."',
                            			  '".$mobile."')";
   $insertQuerydriverdResult = mysql_query($insertQuerydriver);
   if(!$insertQuerydriverdResult)
     die("Insert Query Not Inserted : ".mysql_error());
   else
   {
     //header("Location:drivermaster.php");
   }
}

$truckDataArray = array();
$i = 0;
$selecttruck = "SELECT truckNo, truckId
                  FROM truckmaster
              ORDER BY truckNo";
$selecttruckResult = mysql_query($selecttruck);
while($truckdRow = mysql_fetch_array($selecttruckResult))
{
	$truckDataArray['truckNo'][$i]           = $truckdRow['truckNo'];
	$truckDataArray['truckId'][$i]     = $truckdRow['truckId'];
	
	$i++;
}

$i = 0;
 $selectdriver = "SELECT driverId,licenseNo,driverName,truckNo,address,telephone,mobile
                     FROM driver
                   JOIN truckmaster ON truckmaster.truckId = driver.truckId
                    ORDER BY driverId";
$selectdriverResult = mysql_query($selectdriver);
while($driverDataRow = mysql_fetch_array($selectdriverResult))
{
  $driverArray[$i]['driverId']   = $driverDataRow['driverId'];
  $driverArray[$i]['licenseNo']  = $driverDataRow['licenseNo'];
  $driverArray[$i]['driverName'] = $driverDataRow['driverName'];
  $driverArray[$i]['truckNo']    = $driverDataRow['truckNo'];
  $driverArray[$i]['address']    = $driverDataRow['address'];
  $driverArray[$i]['telephone']  = $driverDataRow['telephone'];
  $driverArray[$i]['mobile']     = $driverDataRow['mobile'];
  $i++;
}

include("./bottom.php");
$smarty->assign("driverArray",$driverArray);  
$smarty->assign("truckDataArray",$truckDataArray);
$smarty->display('driverMaster.tpl');
?>